package com.sam.im.heiqun.utils;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.util.Log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by wangguodong on 2017/9/11.
 */

public class PersimmionsUtils {

    private static final int SDK_PERMISSION_REQUEST_UTIL = 128;

    private Activity activity ;
    private PersimmionsListener persimmionsListener ;

    /**
     * ChatBaseActivity中使用的，
     * 1=拍照；2=相册；3=位置定位；4=小视频拍摄；5=阅后即焚（相册）；6=语音输入；7=语音
     */
    private int type = 0 ;

    public interface PersimmionsListener{
        void OnPersimmionsSuccess(int type);
        void OnPersimmionsFail(int type, int pos, String persimmion);
    }

    public PersimmionsUtils (Activity activity, PersimmionsListener persimmionsListener ){
        this.activity = activity ;
        this.persimmionsListener = persimmionsListener ;
    }

    private static final String TAG = "PersimmionsUtils";

    private void setSuccess(){
        if (null!=persimmionsListener)persimmionsListener.OnPersimmionsSuccess(type);
    }
    private void setFail(int pos, String persimmion){
        if (null!=persimmionsListener)persimmionsListener.OnPersimmionsFail(type, pos, persimmion);
    }

    @TargetApi(23)
    public void getPersimmions(int type, String[] inputpermissions) {
        this.type = type ;
        if (null==inputpermissions || inputpermissions.length <= 0){
            //直接调用成功
            setSuccess();
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            ArrayList<String> permissions = new ArrayList<String>();

            for (int i = 0; i < inputpermissions.length; i++) {
                addPermission(permissions, inputpermissions[i]);
            }
            if (permissions.size() > 0) {
                activity.requestPermissions(permissions.toArray(new String[permissions.size()]), SDK_PERMISSION_REQUEST_UTIL);
            }else {
                setSuccess();
            }
        }else {
//            gotoPhoto();
            //调用成功
            setSuccess();
        }
    }

    @TargetApi(23)
    private boolean addPermission(ArrayList<String> permissionsList, String permission) {
        if (activity.checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) { // 如果应用没有获得对应权限,则添加到列表中,准备批量申请
            if (activity.shouldShowRequestPermissionRationale(permission)) {
                //拒绝了也是可以强行提示的，不过如果再也不提示的情况还没处理
                permissionsList.add(permission);
                return false;
            } else {
                permissionsList.add(permission);
                return false;
            }
        } else {
            return true;
        }
    }

    @TargetApi(23)
//    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
//        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        switch (requestCode) {
            case SDK_PERMISSION_REQUEST_UTIL :
                Log.i(TAG, "getPersimmions: --------007-------permissions.length="+ permissions.length);
                Map<String, Integer> perms = new HashMap<String, Integer>();
                for (int i = 0; i < permissions.length; i++){
                    perms.put(permissions[i], grantResults[i]);
                    Log.i(TAG, "getPersimmions: --------008--------permissions[i]="+permissions[i]);
                    Log.i(TAG, "getPersimmions: --------008--------grantResults[i]="+grantResults[i]);
                    if (-1 == grantResults[i]){
                        setFail(i,permissions[i]);
                        return;
                    }
                }

                setSuccess();

//                try {
//                    int isgre = 0 ;
//                    if (activity.checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
//                        isgre ++;
//                    }
//                    if (activity.checkSelfPermission(Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
//                        isgre ++;
//                    }
//                    if (2 <= isgre){
////                        gotoPhoto();
//                    }else {
////                        Toast.makeText(this, getResources().getString(R.string.disable_picture_permission_will_make_send_picture_unusable), Toast.LENGTH_SHORT)
////                                .show();
//                    }
//                }catch (Exception e){
//                    e.printStackTrace();
//                }
                break;
            default:
//                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        }
    }

}
